<template>
	<view class="">
		<u-tabs :list="list1" style="background-color: #fff;" :current='current' lineWidth='200' :scrollable='false'
			@click="clicktab"></u-tabs>
		<view class="page">
			<view class="peopleitem u-radius-10 u-p-20 flex-between" v-for="i in facelist" v-if="current==0">
				<view class="flex-center u-font-28">
					<image :src="i.avatar" class="w100 h100 u-radius-50" mode=""></image>
					<view class="u-m-l-20">
						<text>{{i.room_addr}}</text>
						<view class="u-m-t-10">{{i.name}}</view>
						<view class="u-m-t-10 flex-between">
							<view class="">
								{{i.create_time}}
							</view>
							<view class=" u-radius-5 u-shrink-no " style="color: #409EFF;" @click="delshow = true,jv_id=i.id">
								删除
							</view>
						</view>
					</view>
				</view>
			</view>

			<u-empty v-if="current==0&&facelist.length==0"></u-empty>

			<view class="flex-center uploadbox u-radius-10" v-if="current==1">
				<view class="flex-between u-font-24 u-m-t-20 " style="width: 90%;" @click="show=true">
					<text>{{address}}</text>
					<u-icon name='arrow-down'></u-icon>
				</view>
				<image src="/static/faceid.jpg" v-if="!preview_url" @click="chooseimg" class="w600 h600 u-m-tb-64"
					mode=""></image>
				<image :src="preview_url" v-else @click="chooseimg" class="w600 h600 u-m-tb-64" mode=""></image>
				<view class="flex-center ">
					<text>备注：</text>
					<input type="text" placeholder="请输入" v-model="name" style="border-bottom: 1rpx solid #000;" />
				</view>

				<view class="role u-radius-5 u-m-tb-60" @click="toupload">
					上传
				</view>
			</view>
		</view>
		<u-modal :show="delshow" :showCancelButton='true' @cancel='delshow=false' @confirm='confirm' :title="title" :content='content'></u-modal>
		<u-picker @confirm="confirm1" @close='show=false' @cancel='show=false' keyName='addr'
			:closeOnClickOverlay='true' :show="show" :columns="unitlist"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				jv_id:'',
				delshow:false,
				show: false,
				current: 0,
				list1: [{
					name: '记录',
				}, {
					name: '注册',
				}, ],
				face_file_id: '',
				preview_url: '',
				name: '',
				facelist: [],
				unitlist: [],
				address: '',
				title:'删除',
				content:'确认删除该用户吗？',
			};
		},
		onLoad() {
			this.getjv()
			this.init()
		},
		methods: {
			confirm1(e) {
				this.show = false
				this.addressid = e.value[0].room_id
				this.address = e.value[0].addr
			},
			//确认删除
			confirm(){
				this.$api.post('/user_face/del_face',{
					form:{
						id:this.jv_id
					}
				}).then(res=>{
					this.delshow = false
					this.getjv()
				})
			},
			init() {
				this.$api.get('/user/device_list').then(res => {
					res.data.map(item => {
						item.addr = item.addr + item.room_name
					})
					this.unitlist = [res.data]

					this.address = res.data[0].addr
					this.addressid = res.data[0].room_id
				})
			},
			clicktab(e) {
				this.current = e.index
				this.getjv()
			},
			getjv() {
				this.$api.get('/user_face/list').then(res => {
					this.facelist = res.data
				})
			},
			chooseimg() {
				var that = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					success: function(res) {
						const tempFilePaths = res.tempFilePaths;

						uni.uploadFile({
							url: that.$uploadUrl,
							filePath: tempFilePaths[0],
							name: "file",

							header: {
								'token': uni.getStorageSync('token')
							},
							success(res) {
								console.log(JSON.parse(res.data));
								that.face_file_id = JSON.parse(res.data).data.fileInfo.file_id
								that.preview_url = JSON.parse(res.data).data.fileInfo.preview_url
							}
						})
					}
				});
			},
			toupload() {
				this.$api.post('/user_face/add_face', {
					form: {
						face_file_id: this.face_file_id,
						name: this.name,
						room_id: this.addressid
					}
				}).then(res => {
					this.getjv()
					this.current = 0
				})
			},
		}
	}
</script>

<style lang="scss">
	.page {
		background-color: #D7E0FB;
		min-height: calc(100vh - var(--window-top) - var(--window-bottom));
		box-sizing: border-box;
		padding: 30rpx;

		.peopleitem {
			background-color: #fff;

			&:nth-child(n+2) {
				margin-top: 20rpx;
			}
		}

		.uploadbox {
			background-color: #fff;
			width: 686rpx;
			flex-direction: column;

			color: #409EFF;
			}

		}
		.role {
			background-color: #E2F2FF;
			padding: 12px 78px;
			
	}
</style>